﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Text;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Service
{
    public class AssetService
    {
        private EKAASBIMEntities dbContext;
        public AssetService()
        {
            dbContext = new EKAASBIMEntities();
        }

        public List<asset> getAllAsset()
        {
            return dbContext.asset.ToList();
        }
        public List<assetType> getAllAssetTypes()
        {
            return dbContext.assetType.Distinct().ToList();
        }
        public List<string> getAllAssetID()
        {
            List<asset> myList = dbContext.asset.ToList();
            List<string> IDlist = new List<string>();
            foreach (asset a in myList)
            {
                IDlist.Add(a.assetID);
            }
            return IDlist;
        }
        public List<string> getAllBelongTo()
        {
            List<employee> myList = dbContext.employee.ToList();
            List<string> BTlist = new List<string>();
            foreach (employee e in myList)
            {
                BTlist.Add(e.alias);
            }
            BTlist = BTlist.Distinct().ToList();
            return BTlist;
        }
        public List<asset> getAllAsset(string alias)
        {
            var query = from a in dbContext.asset
                        where a.belongto != alias
                        select a;
            return query.ToList();
        }
        public assetInfo getAllAssetInfo(string id)
        {
            var query = from a in dbContext.asset
                        join n in dbContext.network on a.assetID equals n.assetID into temp_n
                        from n_l in temp_n.DefaultIfEmpty()
                        where a.assetID == id
                        select new assetInfo
                        {
                            model = a.model,
                            type = a.type,
                            Sn = a.S_n,
                            description = a.description,
                            owner = a.belongto,
                            assetID = a.assetID,
                            MAC = n_l.MAC,
                            status = n_l.status,
                            latestUpdateDate = n_l.latestUpdateDate
                        };
            return query.FirstOrDefault();
        }
        public void insert(assetInfo a, out bool b)
        {
            b = false;
            a.date = DateTime.Now;
            if (dbContext.asset.SingleOrDefault(o => o.assetID == a.assetID) != null)
            {
                b = true;
                return;
            }
            dbContext.asset.AddObject(new asset { assetID = a.assetID, model = a.model, type = a.type, S_n = a.Sn, belongto = a.owner, description = a.description, date = a.date });
            if (string.IsNullOrEmpty(a.owner) == false) dbContext.assetHistory.AddObject(new assetHistory { assetID = a.assetID, empID = a.owner, date = a.date });

            dbContext.SaveChanges();
        }
        public void insertRelatedNetwork(string strMAC, string assetID, out bool b)
        {
            b = false;
            if (dbContext.network.SingleOrDefault(o => o.MAC == strMAC) != null)
            {
                b = true;
                return;
            }
            dbContext.network.AddObject(new network { MAC = strMAC, status = 3, assetID = assetID, latestUpdateDate = new DateTime(2012, 1, 1) });

            dbContext.SaveChanges();
        }
        public IList<assetInfo> delete(string[] ids)
        {
            IList<assetInfo> infos = new List<assetInfo>();
            foreach (string id in ids)
            {
                var d = dbContext.asset.SingleOrDefault(o => o.assetID == id);
                assetInfo info = null;
                if (d != null)
                {
                    if (!string.IsNullOrEmpty(d.belongto))
                    {
                        info = new assetInfo();
                        info.assetID = d.assetID;
                        info.type = d.type;
                        info.model = d.model;
                        info.owner = d.belongto;
                        info.description = d.description;
                        info.Sn = d.S_n;
                    }
                    dbContext.DeleteObject(d);
                }
                var n = dbContext.network.SingleOrDefault(o => o.assetID == id);
                if (n != null)
                {
                    if (info != null)
                    {
                        info.MAC = n.MAC;
                        info.status = n.status;
                    }
                    dbContext.DeleteObject(n);
                }
                if (info != null) infos.Add(info);
                //foreach (var item in dbContext.assetHistory.Where(o => o.assetID == id))
                //{
                //    dbContext.DeleteObject(item);
                //}
            }
            dbContext.SaveChanges();
            return infos;
        }
        public void update(assetInfo asset, out string preOwner)
        {
            preOwner = string.Empty;
            var oldAsset = dbContext.asset.SingleOrDefault(o => o.assetID == asset.assetID);
            if (oldAsset == null)
            {
                return;
            }
            if (oldAsset.belongto != asset.owner)
            {
                dbContext.assetHistory.AddObject(new assetHistory { assetID = asset.assetID, empID = asset.owner, date = DateTime.Now });
                preOwner = oldAsset.belongto;
            }
            oldAsset.model = asset.model;
            oldAsset.type = asset.type;
            oldAsset.description = asset.description;
            oldAsset.belongto = asset.owner;
            oldAsset.S_n = asset.Sn;
            oldAsset.date = DateTime.Now;
            dbContext.SaveChanges();
        }
        public asset updateOwner(string assetID, string owner, out string preOwner)
        {
            preOwner = string.Empty;
            asset oldAsset = dbContext.asset.SingleOrDefault(o => o.assetID == assetID);
            if (oldAsset == null || oldAsset.belongto == owner)
            {
                return null;
            }
            preOwner = oldAsset.belongto;
            dbContext.assetHistory.AddObject(new assetHistory { assetID = assetID, empID = owner, date = DateTime.Now });
            oldAsset.belongto = owner;
            dbContext.SaveChanges();
            return oldAsset;
        }
        public List<asset> getAssetByAlias(string alias)
        {
            var query = from a in dbContext.asset
                        where a.belongto == alias
                        select a;
            return query.ToList();
        }
        public List<assetInfo> getAssetInfoByAlias(string alias)
        {
            var query = from a in dbContext.asset
                        join at in dbContext.assetType on a.type equals at.assetType1
                        join n in dbContext.network on a.assetID equals n.assetID into temp_n
                        from n_l in temp_n.DefaultIfEmpty()
                        where a.belongto == alias
                        select new assetInfo
                        {
                            assetID = a.assetID,
                            owner = a.belongto,
                            model = a.model,
                            type = a.type,
                            hasNIC = at.hasNIC,
                            Sn = a.S_n,
                            description = a.description,
                            date = a.date,
                            status = (n_l.status > 0) ? n_l.status : 0,
                            latestUpdateDate = n_l.latestUpdateDate,
                            MAC = n_l.MAC
                        };
            return query.ToList();
        }
        public IDictionary<asset, IEnumerable<assetHistory>> getAssetHistoryByTimeAndPI(DateTime dt, int pagesize, int index)
        {
            IDictionary<asset, IEnumerable<assetHistory>> histories = new Dictionary<asset, IEnumerable<Database.assetHistory>>();
            var query = (from a in dbContext.asset
                         select a).OrderByDescending(o => o.date).Skip(pagesize * index).Take(pagesize);

            foreach (var a in query)
            {
                var q = (from b in dbContext.assetHistory
                         where b.assetID == a.assetID && b.date > dt
                         select b).ToList();
                if (q.Count > 0) histories.Add(a, q);
            }

            return histories;
        }
        public string getMACByAssetID(string assetID)
        {
            var d = dbContext.network.SingleOrDefault(o => o.assetID == assetID);
            return d.MAC;
        }
    }
}
